package com.app.web.base.util.controller;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import com.app.web.base.model.IdModel;
import com.app.web.base.service.BaseService;

/**
 * Project: erp<br>
 * Description: 公共分页模型，可以套用<br>
 * Copyright (c) 2012<br>
 * All Rights Reserved.<br>
 * @author <a href="mailto:gly420@163.com">GuanLuYong</a><br>
 */
public class PageDateModel<T extends IdModel> extends LazyDataModel<T> {

	private static final long serialVersionUID = 1L;
	
	private List<T> list;
	private BaseService<? extends IdModel> baseService;
	private String prefix;
	private String ttPrefix;
	private String suffix;
	private String orderby;
	private String method;
	private boolean isTruePage = false;	//判断是否为真分页，或假分页
	/**
	 * 构建pageMode对象
	 * @param baseService	调用具体的service
	 * @param prefix		查询前缀
	 * @param ttPrefix		查询总数量前缀
	 * @param suffix		查询后缀
	 * @param method		调用具体的方法，为null则调用默认的search方法
	 */
	public PageDateModel(BaseService<? extends IdModel> baseService, String prefix, String ttPrefix, String suffix, String orderby, String method) {
		this.baseService = baseService;
		this.prefix = prefix;
		this.ttPrefix = ttPrefix;
		this.suffix = suffix;
		this.orderby = orderby;
		this.method = method;
		this.isTruePage = true;
	}
	
	public PageDateModel(List<T> list) {
		this.list = list;
		this.isTruePage = false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> load(int first, int pageSize, String sortField, SortOrder sortOrder,
			Map<String, String> filters) {
		if(!isTruePage) {	//假分页
			int ttSize = list.size();
			setRowCount(ttSize);
			if (ttSize > pageSize) {
				try {
					return list.subList(first, first + pageSize);
				} catch (IndexOutOfBoundsException e) {
					return list.subList(first, first + (ttSize % pageSize));
				}
			}
			return list;  
		}
		
		// 真分页
		if(method == null || "".equals(method)) {
			list = (List<T>) baseService.search(prefix, ttPrefix, suffix, orderby, first, pageSize);
		} else {
			Method[] methods = baseService.getClass().getMethods();
			Object[] args = {prefix, ttPrefix, suffix, first, pageSize};
			for(Method m : methods) {
				if(m.getName().equals(method)) {
					try {
						list = (List<T>) m.invoke(baseService, args);
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					}
					break;
				}
			}
		}
		int ttSize = (int) baseService.getTotalSize().get().longValue();
		setRowCount(ttSize);
		return list;
	}
	
	@Override
	public T getRowData(String rowKey) {
		T obj = null;
		for(T t : list) {
			if(t.getId().toString().equals(rowKey)) {
				obj = t;
				break;
			}
		}
		return obj;
	}

	@Override
	public Object getRowKey(T object) {
		return object.getId();
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

}
